//
//  DateTool.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "QNDateTool.h"
#import "ZZACMacros.h"

//时间转换工具类

@implementation QNDateTool


//时间戳转换成时间(MM月dd日 HH:mm)
+(NSString *)OrderTime:(NSString *)time
{
    if (time.length < 10) {
        return time;
    }
    NSString *times = [time  substringToIndex:10];
    
    long long int date1 = (long long int)[times intValue];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    NSDate *today = [[NSDate alloc] init];
    NSTimeInterval yesterday = -24 * 60 * 60;
    NSDate *mingTian = [today dateByAddingTimeInterval: yesterday];
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * yesterdayString = [[mingTian description] substringToIndex:10];
    
    //1.给NSDate设置时区（不然相差8个小时）
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date2];
    NSDate *localeDate = [date2  dateByAddingTimeInterval: interval];
    NSString * dateString = [[localeDate description] substringToIndex:10];
    
    if ([dateString isEqualToString:todayString])
    {
        [dateFormatter setDateFormat:@"今天 HH:mm"];
    } else if ([dateString isEqualToString:yesterdayString]){
        [dateFormatter setDateFormat:@"昨天 HH:mm"];
    }else{
        [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
    }
    
    NSString *strDate = [dateFormatter stringFromDate:date2];
    return strDate;
}

//时间戳转换成特定格式(yyyy-MM-dd HH:mm)的时间
+(NSString *)getTiemStampToString:(NSString *)time
{
    if (time.length < 10) {
        return time;
    }
    NSString *times = [time  substringToIndex:10];
    
    long long int date1 = (long long int)[times intValue];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:date2];
    return strDate;
}

//时间戳转换成特定格式(yyyy年MM月dd日 HH:mm)的时间
+(NSString *)getTimeToString:(NSString *)time
{
    if (time.length < 10) {
        return time;
    }
    NSString *times = [time  substringToIndex:10];
    
    long long int date1 = (long long int)[times intValue];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:date2];
    return strDate;
}


//时间戳转换成特定格式(yyyy-MM-dd HH:mm:ss)的时间
+(NSString *)getTiemStampToyyMMddHHmmssString:(NSString *)time
{
    if (time.length < 10) {
        return time;
    }
    NSString *times = [time  substringToIndex:10];
    
    long long int date1 = (long long int)[times intValue];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:date2];
    return strDate;
}


//IOS判断日期是今天、后天还是明天
+(NSString *)compareDate:(NSDate *)date
{
    //解决时间相差8小时问题====原因: 使用 NSDate *date = [NSDate date]; 获取的时间是标注的UTC时间,和北京时间相差8小时.将UTC时间转成当地的时间只需要设置  NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSTimeInterval thirdsPerDay = 48 * 60 * 60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *tomorrow, *houTian;
    
    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    houTian = [today dateByAddingTimeInterval: thirdsPerDay];
    
    
    // 10 first characters of description is the calendar date:
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * houTianString = [[houTian description] substringToIndex:10];
    NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
    
    //1.给NSDate设置时区（不然相差8个小时）
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    NSString * dateString = [[localeDate description] substringToIndex:10];
    
    if ([dateString isEqualToString:todayString])
    {
        return @"今天";
    } else if ([dateString isEqualToString:houTianString])
    {
        return @"后天";
    }else if ([dateString isEqualToString:tomorrowString])
    {
        return @"明天";
    }
    else
    {
        return dateString;
    }
}


//时间戳转换成特定格式的时间 ( XX月XX日 明天 15:00 )
+(NSString *)getTiemStampToSpecialString:(NSString *)time
{
    if (time.length < 10) {
        return time;
    }
    NSString *times = [time  substringToIndex:10];
    
    long long int date1 = (long long int)[times intValue];
    
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:date2];
    //替换字符串中的空格
    NSString *specialStr = [strDate stringByReplacingOccurrencesOfString:@" " withString:[NSString stringWithFormat:@" %@ ",[QNDateTool compareDate:date2]]];
    return specialStr;
}


//将date转换成时间戳
+(NSString *)transDateToTimesp:(NSString *)timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate* date = [formatter dateFromString:timeStr]; //将字符串按formatter转成nsdate
    
    //NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    GCLog(@"timeSp:%@",timeSp); //时间戳的值（精确到毫秒的）
    return timeSp;
    
}


//将返回的 "yyyy-MM-dd HH:mm:ss" 的格式转换为 yyyyMMddHHmmss格式
+(NSString *)transDateFormate:(NSString *) timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate* date = [formatter dateFromString:timeStr];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *strDate = [dateFormatter stringFromDate:date];
    return strDate;
}


//将 yyyyMMddHHmmss 的格式转换为 yyyy-MM-dd HH:mm:ss 格式
+(NSString *)transDateFormateyyyyMMddHHmmss:(NSString *) timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    
    NSDate* date = [formatter dateFromString:timeStr];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:date];
    return strDate;
}

//时间格式转化  yyyy-MM-dd HH:mm:ss转换为 yyyy-MM-dd HH:mm
+(NSString *)formatDateWithString:(NSString *)str
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:str];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *timeStr = [formatter stringFromDate:date];
    return timeStr;
}


//将日期2016-06-01 转换成月份 6
+(NSInteger)transDateToMonth:(NSString *)str
{
    //    NSString *dateStr= @"2016-04-01 00:01:00";
    
    NSString *dateStr= str;
    NSDateFormatter *nextformatter = [[NSDateFormatter alloc] init] ;
    [nextformatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *date=[nextformatter dateFromString:dateStr];
    //    [nextformatter setDateFormat:@"yyyy"];
    //    NSInteger year=[[nextformatter stringFromDate:date] integerValue];
    [nextformatter setDateFormat:@"MM"];
    NSInteger month=[[nextformatter stringFromDate:date]integerValue];
    //    [nextformatter setDateFormat:@"dd"];
    //    NSInteger day=[[nextformatter stringFromDate:date] integerValue];
    //    GCLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,year,month,day);
    
    return month;
}


//将日期2016-06-01转换成日期 1
+(NSInteger)transDateToDay:(NSString *)str
{
    //    NSString *dateStr= @"2016-04-01 00:01:00";
    
    NSString *dateStr= str;
    NSDateFormatter *nextformatter = [[NSDateFormatter alloc] init] ;
    [nextformatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *date=[nextformatter dateFromString:dateStr];
    //    [nextformatter setDateFormat:@"yyyy"];
    //    NSInteger year=[[nextformatter stringFromDate:date] integerValue];
    //    [nextformatter setDateFormat:@"MM"];
    //    NSInteger month=[[nextformatter stringFromDate:date]integerValue];
    [nextformatter setDateFormat:@"dd"];
    NSInteger day=[[nextformatter stringFromDate:date] integerValue];
    //    GCLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,year,month,day);
    
    return day;
}


//将日期2016-06-01 00:00:00 转换成月份 6
+(NSInteger)transDate2ToMonth:(NSString *)str
{
    NSString *dateStr= str;
    NSDateFormatter *nextformatter = [[NSDateFormatter alloc] init] ;
    [nextformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date=[nextformatter dateFromString:dateStr];
    [nextformatter setDateFormat:@"MM"];
    NSInteger month=[[nextformatter stringFromDate:date]integerValue];
    return month;
}


//将日期2016-06-01 00:00:00 转换成日期 1
+(NSInteger)transDate2ToDay:(NSString *)str
{
    NSString *dateStr= str;
    NSDateFormatter *nextformatter = [[NSDateFormatter alloc] init] ;
    [nextformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date=[nextformatter dateFromString:dateStr];
    [nextformatter setDateFormat:@"dd"];
    NSInteger day=[[nextformatter stringFromDate:date] integerValue];
    return day;
}


//iOS 获取网络时间(百度时间)
+ (NSString *)getInternetDate
{
    NSString *urlString = @"http://m.baidu.com";
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    
    // 实例化NSMutableURLRequest，并进行参数配置
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
    [request setURL:[NSURL URLWithString: urlString]];
    
    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    
    [request setTimeoutInterval: 2];
    
    [request setHTTPShouldHandleCookies:FALSE];
    
    [request setHTTPMethod:@"GET"];
    
    NSError *error = nil;
    
    NSHTTPURLResponse *response;
    
    [NSURLConnection sendSynchronousRequest:request
     
                          returningResponse:&response error:&error];
    
    // 处理返回的数据
    
    //    NSString *strReturn = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    
    if (error) {
        NSDate *date = [NSDate date];
        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
        return timeSp;
    }
    
    //    GCLog(@"response is %@",response);
    
    NSString *date = [[response allHeaderFields] objectForKey:@"Date"];
    
    date = [date substringFromIndex:5];
    
    date = [date substringToIndex:[date length]-4];
    
    NSDateFormatter *dMatter = [[NSDateFormatter alloc] init];
    
    dMatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    [dMatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];
    
    NSDate *netDate = [[dMatter dateFromString:date] dateByAddingTimeInterval:60*60*8];
    NSString *timeNet = [NSString stringWithFormat:@"%ld", (long)[netDate timeIntervalSince1970]];
    return timeNet;
    
}



//获取当前时间并以时间戳形式返回(当前时间为手机时间)
+(NSString *)getNSDateTotransTimeStamp
{
    //获取当前时间 （精确到秒的）
    //    NSDate *date = [NSDate date];
    //    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    
    //（精确到毫秒的）
    NSTimeInterval nowtime = [[NSDate date] timeIntervalSince1970]*1000;
    long long theTime = [[NSNumber numberWithDouble:nowtime] longLongValue];
    NSString *timeSp = [NSString stringWithFormat:@"%llu",theTime];
    
    GCLog(@"timeSp:%@",timeSp); //时间戳的值
    return timeSp;
    
}


//获取当前时间并以时间戳形式返回（精确到秒）
+(NSString *)getNSDateToSecond
{
    //获取当前时间 （精确到秒的）
    NSDate *date = [NSDate date];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    GCLog(@"timeSp:%@",timeSp); //时间戳的值
    return timeSp;
}


//获取当前时间并以格式yyyy-MM-dd HH:mm:ss
+(NSString *)getNSDatetransToFormate
{
    //获取当前时间
    NSDate *date = [NSDate date];
    
    //    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date];//前一天
    //    NSDate *nextDat = [NSDate dateWithTimeInterval:24*60*60 sinceDate:date];//后一天
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *time1 =[dateFormatter stringFromDate:date];
    return time1;
}


//获取今天、明天、后天的时间并以格式yyyy-MM-dd
+(NSString *)getNSDatetransToFormateWith:(NSString *)str
{
    NSString *time;
    //获取当前时间
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    if ([str isEqualToString:@"今天"]) {
        time =[dateFormatter stringFromDate:date];
    }else if ([str isEqualToString:@"明天"]){
        NSDate *nextDat = [NSDate dateWithTimeInterval:24*60*60 sinceDate:date];//明天
        time =[dateFormatter stringFromDate:nextDat];
    }else if ([str isEqualToString:@"后天"]){
        NSDate *thirdDat = [NSDate dateWithTimeInterval:48*60*60 sinceDate:date];//后天
        time =[dateFormatter stringFromDate:thirdDat];
    }
    return time;
}


//两个时间戳相减得到时间（单位秒）
+(NSString *)getSecondWithStartTime:(NSString *)startTime AndEndTime:(NSString *)endTime
{
    long long startTimeTip = [startTime longLongValue];
    long long endTimeTip = [endTime longLongValue];
    long long time = endTimeTip - startTimeTip;
    int minute= (int)time/60;
    int second=time%60;
    int countSec = minute*60 + second;
    NSString *secStr = [NSString stringWithFormat:@"%d",countSec];
    return secStr;
}





@end
